/**
 * <p>文件名:		DynamicProxyService.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		CompanyTag</p>
 * @author		周华彬(zhouhuabin@hayll.cn, zhou_hua_bin@163.com)
*/

package com.caits.lbs.framework.services.impl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.logging.log4j.LogManager;

import com.caits.lbs.framework.utils.BizUtils;

/**
 * <p>
 * DynamicProxyService
 * </p>
 * <p>
 * 通用动态代理样例
 * </p>
 *
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>Administrator</td>
 *          <td>2020年8月25日 下午3:02:30</td>
 *          </tr>
 *          <tr>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          <td>XXX</td>
 *          </tr>
 *          </table>
 */
public class DynamicProxyServiceImpl implements InvocationHandler {
	/** 变量:TODO,类型:Logger */
	final static org.apache.logging.log4j.Logger SLOG =   LogManager.getLogger(DynamicProxyServiceImpl.class);
	private Object delegate;

	public Object bind(Object delegate) {
		this.delegate = delegate;
		return Proxy.newProxyInstance(this.delegate.getClass().getClassLoader(), this.delegate.getClass().getInterfaces(), this);
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object result = null;
		try {
			SLOG.info("问候之前的日志记录...");
			// JVM通过这条语句执行原来的方法(反射机制)
			result = method.invoke(this.delegate, args);
		} catch (Exception e) {
			BizUtils.printShortException("代理调用", e);
		}
		return result;
	}
}
